package com.ozo.easy.controller;

import com.ozo.easy.common.model.*;
import com.ozo.easy.common.log.annotation.LogAnno;
import com.ozo.easy.pojo.dto.SysRoleSaveDTO;
import com.ozo.easy.pojo.dto.SysRoleQueryDTO;
import com.ozo.easy.pojo.vo.SysRoleVO;
import com.ozo.easy.service.ISysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;

/**
 * 角色 前端控制器
 *
 * @author ozo
 * @version 1.0
 * @date 2025-02-14 16:53:43
 */
@Tag(name = "角色")
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    private final ISysRoleService sysRoleService;

    @Operation(summary = "表格页")
    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("sysRole/index");
        return modelAndView;
    }

    @Operation(summary = "添加页")
    @GetMapping("/addForm")
    public ModelAndView addForm() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("sysRole/addForm");
        return modelAndView;
    }

    @Operation(summary = "编辑页")
    @GetMapping("/editForm/{id}")
    public ModelAndView editForm(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("data", sysRoleService.detail(id));
        modelAndView.setViewName("sysRole/editForm");
        return modelAndView;
    }

    @LogAnno(value = "添加角色", module = "角色")
    @Operation(summary = "添加角色")
    @PostMapping("/add")
    public ResultVO<Void> add(@RequestBody @Valid SysRoleSaveDTO saveDTO) {
        sysRoleService.add(saveDTO);
        return ResultVO.success();
    }

    @LogAnno(value = "删除角色", module = "角色")
    @Operation(summary = "删除角色")
    @DeleteMapping("/remove")
    public ResultVO<Void> remove(@RequestBody @Valid BaseBatchDTO<Long> batchDTO) {
        sysRoleService.remove(batchDTO);
        return ResultVO.success();
    }

    @LogAnno(value = "编辑角色", module = "角色")
    @Operation(summary = "编辑角色")
    @PutMapping("/edit")
    public ResultVO<Void> edit(@RequestBody @Validated(value = BaseValidDTO.Edit.class) SysRoleSaveDTO saveDTO) {
        sysRoleService.edit(saveDTO);
        return ResultVO.success();
    }

    @Operation(summary = "角色分页")
    @GetMapping("/page")
    public ResultVO<PageResult<SysRoleVO>> page(@Valid BasePage basePage, SysRoleQueryDTO queryDTO) {
        return ResultVO.success(sysRoleService.page(basePage, queryDTO));
    }

    @Operation(summary = "启用禁用")
    @PutMapping("/enable/{id}")
    public ResultVO<Void> enable(@PathVariable Long id) {
        sysRoleService.enable(id);
        return ResultVO.success();
    }

}